#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node
{
    char intStr[100];
    struct Node* next;
} Node;


void doCondense(char* intStrSrc, char* intStrDest)
{
    int i = 0;
    int* pIntCount = NULL;
    char strTemp[100];

    pIntCount = (int*)malloc(10 * sizeof(int));
    for(i = 0; i <10; i++)
        pIntCount[i] = 0;

    for(i = 0; i < strlen(intStrSrc); i++)
    {
        pIntCount[intStrSrc[i] - '0']++;
    }
    if(intStrDest == NULL)
        return;
    strcpy(intStrDest, "\0");
    for(i = 0; i <10; i++)
    {
        if(pIntCount[i] != 0)
        {
            sprintf(strTemp, "%d%d", pIntCount[i], i);
            strcat(intStrDest, strTemp);
        }
    }

    free(pIntCount);
    pIntCount = NULL;

}
int testSelfInventory(char* intStr)
{
    char intStrCondensed[100] = "";

    int nStep = 0, nCount = 0;
    Node *head = NULL, *plast = NULL, *p = NULL, *pnew = NULL;

    head = (Node*)malloc(sizeof(Node));
    strcpy(head->intStr, intStr);
    head->next = NULL;

    plast = head;
    while(plast != NULL)
    {
        doCondense(plast->intStr, intStrCondensed);
        //
        nStep = 0;
        for(p = head;  p != NULL && strcmp(p->intStr, intStrCondensed) != 0; p = p->next)
            nStep++;

        if(nStep >= 15)
        {
            printf("%s can not be classified after 15 iterations\n", intStr);
            break;
        }


        if(p == NULL)
        {
            pnew = (Node*)malloc(sizeof(Node));
            strcpy(pnew->intStr, intStrCondensed);
            pnew->next = NULL;
            plast->next = pnew;
            nCount++;
        }

        else
        {
            if(nStep == nCount && nStep == 0)
            {

                printf("%s is self-inventorying\n", intStr);
            }
            else if(nStep == nCount && nStep != 0)
            {
                printf("%s is self-inventorying after %d steps\n", intStr, nStep);
            }
            else if(nStep  != nCount)
            {
                printf("%s enters an inventory loop of length %d\n", intStr, nCount - nStep + 1);
            }
            break;
        }

        plast = plast->next;
    }

    return 0;
}


int main()
{
    char intStr[100];


    scanf("%s", intStr);
    while(strcmp(intStr, "-1") != 0)
    {
        testSelfInventory(intStr);

        scanf("%s", intStr);
    }

    return 0;
}
